#include "curve.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#ifdef _MAC_OS_
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif

using namespace std;

Curve::Curve(){
	string line;
	ifstream myfile ("sinus1.curv");
	if (myfile.is_open()){
		while ( getline (myfile,line) ){
			float x, y;
			std::istringstream linestream (line);
			linestream >> x; linestream >> y;
			vec2f v = vec2f(x);
			v[1] = y;
			_coord.push_back(v);
			_max[0]=max(_max[0], x);
			_max[1]=max(_max[1], y);
			_min[0]=min(_min[0], x);
			_min[1]=min(_min[1], y);
		}
		myfile.close();
		
		vec2f dim = (_max-_min);
		vec2f center = (_max+_min)/2;
		for (unsigned int i=0; i<_coord.size(); i++){
			_coord[i] = _coord[i] - center;
			_coord[i][0] = 2*_coord[i][0]/(dim[0]);
			_coord[i][1] = 2*_coord[i][1]/(dim[1]);
		}
		
		
	}else{
		cout << "Unable to open file";
	} 
	_dim = vec2f(0.5);
};

Curve::~Curve(){
};

void Curve::setDim(vec2f v){
    _dim = v;
};

const vec2f& Curve::getDim() const{
    return _dim;
};

vec2f& Curve::getDim(){
    return _dim;
};

void Curve::draw() const{
  glColor3f(_color[0],
            _color[1],
            _color[2]);

 std::vector<vec2f>::const_iterator it;
    for (it=_coord.begin(); it<_coord.end()-1; it++){
    	glBegin (GL_LINES);
    		glVertex2f ((_pos[0]+(*(it))[0])*_dim[0], (_pos[1]+(*it)[1])*_dim[1]);
			glVertex2f ((_pos[0]+(*(it+1))[0])*_dim[0], (_pos[1]+(*(it+1))[1])*_dim[1]);
			
	glEnd ();

    }

	

};




/*

	std::tr1::cmatch res;
    str = "<h2>Egg prices</h2>";
    std::tr1::regex rx("<h(.)>([^<]+)");
    std::tr1::regex_search(str.c_str(), res, rx);
    std::cout << res[1] << ". " << res[2] << "\n";
*/
